package desmontec.documentos


class GuiaDeTrafegoController {
	
	GuiaDeTrafegoService guiaDeTrafegoService
	DpgService dpgService
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [guiaDeTrafegoInstanceList: GuiaDeTrafego.list(params), guiaDeTrafegoInstanceTotal: GuiaDeTrafego.count()]
    }

    def create = {
        def guiaDeTrafegoInstance = new GuiaDeTrafego()
        guiaDeTrafegoInstance.properties = params
        return [guiaDeTrafegoInstance: guiaDeTrafegoInstance]
    }

    def save = {
        def guiaDeTrafegoInstance = new GuiaDeTrafego(params)
        if (guiaDeTrafegoInstance.save(flush: true)) {
			//fernando.alves - inicio
			dpgService.atualizaValidadeDPG(guiaDeTrafegoInstance.mapaDeEstocagem.dpg)
			//fernando.avles - fim
            flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.created.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), guiaDeTrafegoInstance.id])}"
            redirect(action: "show", id: guiaDeTrafegoInstance.id)
        }
        else {
            render(view: "create", model: [guiaDeTrafegoInstance: guiaDeTrafegoInstance])
        }
    }

    def show = {
        def guiaDeTrafegoInstance = GuiaDeTrafego.get(params.id)
        if (!guiaDeTrafegoInstance) {
            flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.not.found.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
            redirect(action: "list")
        }
        else {
            [guiaDeTrafegoInstance: guiaDeTrafegoInstance]
        }
    }

    def edit = {
        def guiaDeTrafegoInstance = GuiaDeTrafego.get(params.id)
        if (!guiaDeTrafegoInstance) {
            flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.not.found.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [guiaDeTrafegoInstance: guiaDeTrafegoInstance]
        }
    }

    def update = {
        def guiaDeTrafegoInstance = GuiaDeTrafego.get(params.id)
//		System.out.println("NO UPDATE ID - "+params.id+" "+guiaDeTrafegoInstance)
        if (guiaDeTrafegoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (guiaDeTrafegoInstance.version > version) {
//                  System.out.println(" ENTREI NO IF DO VERSION, VOU REJEITAR")
                    guiaDeTrafegoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego')] as Object[], "Another user has updated this GuiaDeTrafego while you were editing")
                    render(view: "edit", model: [guiaDeTrafegoInstance: guiaDeTrafegoInstance])
                    return
                }
            }
            guiaDeTrafegoInstance.properties = params
//			System.out.println(" PARAMETROS NO UPDATE "+guiaDeTrafegoInstance.dinamite)
            if (!guiaDeTrafegoInstance.hasErrors() && guiaDeTrafegoInstance.save(flush: true)) {
				//fernando.alves - inicio
				dpgService.atualizaValidadeDPG(guiaDeTrafegoInstance.mapaDeEstocagem.dpg)
				//fernando.avles - fim
                flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.updated.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), guiaDeTrafegoInstance.id])}"
                redirect(action: "show", id: guiaDeTrafegoInstance.id)
            }
            else {
                render(view: "edit", model: [guiaDeTrafegoInstance: guiaDeTrafegoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def guiaDeTrafegoInstance = GuiaDeTrafego.get(params.id)
		
        if (guiaDeTrafegoInstance) {
            try {
				
				//fernando.alves - inicio
				def dpgReferente = guiaDeTrafegoInstance.mapaDeEstocagem.dpg
				guiaDeTrafegoInstance.delete(flush: true)
				
				dpgService.atualizaValidadeDPG(dpgReferente)
				
				flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.deleted.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
				
				
				//fernando.alves - fim
				redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.not.deleted.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'desmontec.documentos.guiadetrafego.default.not.found.message', args: [message(code: 'guiaDeTrafego.label', default: 'GuiaDeTrafego'), params.id])}"
            redirect(action: "list")
        }
    }
	
	static final Integer RESPOSTA_PARAMETROS_INVALIDOS = 1;
	static final Integer RESPOSTA_LIMITE_EXCEDIDO = 2;
	static final Integer RESPOSTA_SUCESSO = 3;
	
	//fernando.alves - inicio
	def validarGuia = {
//		System.out.println(" ANTES DO DEF")
		def guiaDeTrafegoInstance
		if (params.idGuia != null){
			guiaDeTrafegoInstance = GuiaDeTrafego.get(Long.parseLong(params.idGuia))
			guiaDeTrafegoInstance.properties = params
		} else {
			guiaDeTrafegoInstance = new GuiaDeTrafego(params)
		}
		
//		System.out.println("GUIA NO VALIDAR ")
//		//Long.parseLong(params.idGuia)
//		System.out.println(" ENTREI NO VALIDAR GUIA: ID = "+guiaDeTrafegoInstance.id+" TO STRING "+guiaDeTrafegoInstance.toString())
		
		if(!guiaDeTrafegoInstance.validate()){
			
		} else if(!guiaDeTrafegoService.validarMateriaisDaGuia(guiaDeTrafegoInstance)){
//			System.out.println(" RESULTADO DO VALIDAR - EXCEDIDO")
			return [resposta: RESPOSTA_LIMITE_EXCEDIDO]
		}
//		System.out.println(" RESULTADO DO VALIDAR - OK")
		return [resposta: RESPOSTA_SUCESSO]
	}
	//fernando.alves - fim
}
